# -*- coding: utf-8 -*-
# created on 2018/05/16

from mathsolver.functions.base import *


class DengBiXingZhiEqUpdate001(BaseFunction):
    """
    记等差数列{a_{n}}的前n项和为S_{n},设S_{3}=12,2a_{1},a_{2},a_{3}+1成等比数列,求S_{n}.
    """
    def solver(self, *args):
        assert len(args) == 1
        left, middle, right = args[0].sympify()
        steps = [["", "∵%s, %s, %s成等比数列" % (new_latex(left), new_latex(middle), new_latex(right))],
                 ["", "∴%s" % (BaseEq([middle ** 2, left * right]).printing())],
                 ["", "∴ %s, %s, %s" % (BaseIneq([left, "!=", S.Zero]).printing(),
                                        BaseIneq([middle, "!=", S.Zero]).printing(),
                                        BaseIneq([right, "!=", S.Zero]).printing())]]
        condition_eqs = [[left, "!=", S.Zero], [middle, "!=", S.Zero], [right, "!=", S.Zero],
                         [middle ** 2, left * right]]
        answer = {}
        if 'Sequations' in self.known:
            seqs = self.search('Sequations')
        else:
            seqs = []
        for eq in condition_eqs:
            seqs.append(eq)
        answer['Sequations'] = seqs
        self.output.append(BaseShuLieValue(answer))
        self.steps.extend([[a, b] for a, b in steps])
        return self


class DengBiXingZhiEqUpdate002(BaseFunction):
    """
    在等差数列{a_{n}}中,a_{1}+a_{3}=8,且a_{4}为a_{2}和a_{9}的等比中项,求a_{n}.
    """
    def solver(self, *args):
        assert len(args) == 3
        left = args[0].sympify()
        middle = args[1].sympify()
        right = args[2].sympify()
        self.steps.append(["", "∵%s, %s, %s成等比数列" % (new_latex(left), new_latex(middle), new_latex(right))])
        eq = [middle ** 2, left * right]
        steps = [["", "∴%s" % (BaseEq(eq).printing())]]
        condition_eqs = [[left, "!=", S.Zero], [middle, "!=", S.Zero], [right, "!=", S.Zero],
                         [middle ** 2, left * right]]
        answer = {}
        if 'Sequations' in self.known:
            seqs = self.search('Sequations')
        else:
            seqs = []
        for eq in condition_eqs:
            seqs.append(eq)
        answer['Sequations'] = seqs
        self.output.append(BaseShuLieValue(answer))
        self.steps.extend(steps)
        self.label.add("等比中项满足的条件")
        return self


class DengBiXingZhiEqUpdate003(BaseFunction):
    """
    数列{a_{n}}是等差数列,若a_{1}+1,a_{3}+3,a_{5}+5构成公比为q的等比数列,则q=.
    """
    def solver(self, *args):
        assert len(args) == 2
        left, middle, right = args[0].sympify()
        self.label.add("等比中项满足的条件")
        gongbi = args[1].sympify()
        answer = {}
        if 'Sequations' in self.known:
            seqs = self.search('Sequations')
        else:
            seqs = []
        seqs.append([left, "!=", S.Zero])
        seqs.append([middle, "!=", S.Zero])
        seqs.append([right, "!=", S.Zero])
        seqs.append([gongbi, "!=", 0])
        seqs.append([left * gongbi, middle])
        seqs.append([left * gongbi ** 2, right])
        answer['Sequations'] = seqs
        self.output.append(BaseShuLieValue(answer))
        return self


class DengBiXingZhiEqUpdate004(BaseFunction):
    """
    若2\\sqrt{2}是b-1,b+1的等比中项,则b=.
    """
    def solver(self, *args):
        assert len(args) == 2
        assert not isinstance(args[1], str)
        middle = args[0].sympify()
        left, right = args[1].sympify()
        answer = {}
        if 'Sequations' in self.known:
            seqs = self.search('Sequations')
        else:
            seqs = []
        seqs.append([left, "!=", S.Zero])
        seqs.append([middle, "!=", S.Zero])
        seqs.append([right, "!=", S.Zero])
        seqs.append([left * right, middle ** 2])
        answer['Sequations'] = seqs
        self.output.append(BaseShuLieValue(answer))
        self.label.add("等比中项满足的条件")
        return self


class DengBiXingZhiEqUpdate005(BaseFunction):
    """
    若三个正数a,b,c成等比数列,其中a=5+2\\sqrt 6,c=5-2\\sqrt 6,则b=.
    """
    def solver(self, *args):
        assert len(args) == 2
        assert isinstance(args[0], BaseMultiple) and args[0].type == 'Variables'
        assert isinstance(args[1], str)
        left, middle, right = args[0].sympify()
        seqs = []
        text = args[1]
        if text.find("正") >= 0:
            seqs.append([left, ">", S.Zero])
            seqs.append([middle, ">", S.Zero])
            seqs.append([right, ">", S.Zero])
        else:
            seqs.append([left, "!=", S.Zero])
            seqs.append([middle, "!=", S.Zero])
            seqs.append([right, "!=", S.Zero])
        seqs.append([left * right, middle ** 2])
        self.output.append(BaseIneqs(seqs))
        self.label.add("等比中项满足的条件")
        return self


class DengBiXingZhiEqUpdate(BaseFunction):
    CLS = [DengBiXingZhiEqUpdate001, DengBiXingZhiEqUpdate002, DengBiXingZhiEqUpdate003,
           DengBiXingZhiEqUpdate004, DengBiXingZhiEqUpdate005]

    def solver(self, *args):
        r = None
        for cl in DengBiXingZhiEqUpdate.CLS:
            try:
                r = cl(self.known, verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r
